With an action as follows :- public class MyAction extends ActionSupport { ... private List<LocalDate> selectedPurchasePeriods = new ArrayList(); ... public void setSelectedPurChasePeriodes(List<LocalDate> periods) { this.selectedPurchasePeriods = periods; } .... } To allow conversion to take place, we probably needs to write up a converter, such that when a requiest such as http://www.domain.com/context/myAction.action?selectedPurchasePeriods=2007-02&selectedPurchasePeriods=2007-03
will results in the List<LocalDate> being populated with 2 dates. Here's how the converter might look like (notes are inserted as comments in the code) /** * @author martin.gilday * */ public class YearMonthToLocalDateConverter extends WebWorkTypeConverter { private Logger log = Logger.getLogger(YearMonthToLocalDateConverter.class); private static final DateTimeFormatter YEAR_MONTH_FORMAT = DateTimeFormat.forPattern("yyyy-MM"); @Override public Object convertFromString(Map context, String[] values, Class toClass) { try { // Since the assiciated property in WebWork action is List<LocalDate>, we'll need to // return result of the correct type as well, maybe something like return new ArrayList() { { add(YEAR_MONTH_FORMAT.parseDateTime(values[0]).toLocalDate()); add(YEAR_MONTH_FORMAT.parseDateTime(values[1]).toLocalDate()); } }; } catch (Exception e) { log.error("FAILURE TO CONVERT", e); throw new TypeConversionException("Unable to convert to LocalDate"); } } @Override public String convertToString(Map context, Object o) { try { // We might want to return a sensible string such that when we use eg. <ww:property ... /> tag, // sensible string representation gets printed out, Object o is castable to List<LocalDate>. .... } catch (Exception e) { throw new TypeConversionException("Unable to convert LocalDate to String"); } } }
|